package com.foreverwin.jzymes.qc.point.service.impl;

import com.foreverwin.jzymes.common.util.BOHandleUtil;
import com.foreverwin.jzymes.common.util.CommonMethods;
import com.foreverwin.jzymes.framework.util.I18nUtil;
import com.foreverwin.jzymes.qc.point.domain.PointChecklist;
import com.foreverwin.jzymes.qc.point.enums.PointCheckTypeEnum;
import com.foreverwin.jzymes.qc.point.service.IDoPointCheckService;
import com.foreverwin.jzymes.qc.point.service.IPointChecklistService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.*;

@Service
public class DoPointCheckServiceImpl implements IDoPointCheckService {
    @Autowired
    IPointChecklistService pointChecklistService;

    /**
     * 样品点检记录
     * @param jsonMap
     */
    @Override
    public void doSamplePointCheck(List<Map<String,Object>> jsonMap) {

        for ( int i = 0; i<jsonMap.size();i++ ){
            Assert.notNull( jsonMap.get(i).get("checkResult"), I18nUtil.getI18nText("qc.point.checkResultisnull"));

            PointChecklist pointCheck = new PointChecklist();

            pointCheck.setHandle( BOHandleUtil.buildPointCheckHandle() );
            pointCheck.setType( PointCheckTypeEnum.SAMPLE.getValue() );
            pointCheck.setKey( (String) jsonMap.get(i).get("handle") );
            pointCheck.setCheckResult( (String) jsonMap.get(i).get("checkResult") );
            pointCheck.setRemark( (String) jsonMap.get(i).get("remark") );
            pointCheck.setStatus( (String) jsonMap.get(i).get("status") );
            pointCheck.setChecker( CommonMethods.getUserBo() );
            pointCheck.setCheckTime( new Date() );

            pointChecklistService.insertPointChecklist( pointCheck );
        }
    }

    /**
     * 检具点检记录
     * @param jsonMap
     */
    @Override
    public void doToolPointCheck(List<Map<String, Object>> jsonMap) {
        for ( int i = 0; i<jsonMap.size();i++ ){
            Assert.notNull( jsonMap.get(i).get("checkResult"), I18nUtil.getI18nText("qc.point.checkResultisnull"));

            PointChecklist pointCheck = new PointChecklist();
            pointCheck.setHandle( BOHandleUtil.buildPointCheckHandle() );
            pointCheck.setType( PointCheckTypeEnum.TOOL.getValue() );
            pointCheck.setKey( (String) jsonMap.get(i).get("handle") );
            pointCheck.setCheckResult( (String) jsonMap.get(i).get("checkResult") );
            pointCheck.setRemark( (String) jsonMap.get(i).get("remark") );
            pointCheck.setChecker( CommonMethods.getUserBo() );
            pointCheck.setCheckTime( new Date() );
            String status = (String) jsonMap.get(i).get("status");
            if ( status.equals("提醒")){
                pointCheck.setStatus( "R" );
            }else if ( status.equals("已启用") ){
                pointCheck.setStatus( "O" );
            }else if ( status.equals("逾期") ){
                pointCheck.setStatus( "U" );
            }
            pointChecklistService.insertPointChecklist( pointCheck );
        }
    }
}
